<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">

        <!--        <fieldset class="table-search-fieldset">-->
        <!--            <legend>搜索信息</legend>-->
        <!--            <div style="margin: 10px 10px 10px 10px">-->
        <!--                <form class="layui-form layui-form-pane" action="">-->
        <!--                    <div class="layui-form-item">-->
        <!--                        <div class="layui-inline">-->
        <!--                            <label class="layui-form-label">用户姓名</label>-->
        <!--                            <div class="layui-input-inline">-->
        <!--                                <input type="text" name="username" autocomplete="off" class="layui-input">-->
        <!--                            </div>-->
        <!--                        </div>-->
        <!--                        <div class="layui-inline">-->
        <!--                            <label class="layui-form-label">用户性别</label>-->
        <!--                            <div class="layui-input-inline">-->
        <!--                                <input type="text" name="sex" autocomplete="off" class="layui-input">-->
        <!--                            </div>-->
        <!--                        </div>-->
        <!--                        <div class="layui-inline">-->
        <!--                            <label class="layui-form-label">用户城市</label>-->
        <!--                            <div class="layui-input-inline">-->
        <!--                                <input type="text" name="city" autocomplete="off" class="layui-input">-->
        <!--                            </div>-->
        <!--                        </div>-->
        <!--                        <div class="layui-inline">-->
        <!--                            <label class="layui-form-label">用户职业</label>-->
        <!--                            <div class="layui-input-inline">-->
        <!--                                <input type="text" name="classify" autocomplete="off" class="layui-input">-->
        <!--                            </div>-->
        <!--                        </div>-->
        <!--                        <div class="layui-inline">-->
        <!--                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit-->
        <!--                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索-->
        <!--                            </button>-->
        <!--                        </div>-->
        <!--                    </div>-->
        <!--                </form>-->
        <!--            </div>-->
        <!--        </fieldset>-->

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加</button>
                <!--                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>-->
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
<!--            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="relevancy">关联</a>-->
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit gen_corpus"
               lay-event="gen_corpus">生成语料</a>
            <!--            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>-->
        </script>

    </div>
</div>

<script>
    layui.use(['form', 'table', 'miniPage', 'element'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            miniPage = layui.miniPage;

        table.render({
            elem: '#currentTableId',
            url: 'admin/modulesList/2',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                // {type: "checkbox", width: 50},
                {field: 'id', width: 30, title: 'ID', sort: true},
                {field: 'name', width: 220, title: '模型名称', align: "center"},
                // {field: 'con_name', width: 120, title: '语境库名称', align: "center"},
                {field: 'ctx_len', width: 80, title: '上下文长度', align: "center"},
                {field: 'n_epoch', title: '训练轮数', width: 80, align: "center"},
                {field: 'module_code', title: '标记码', width: 200, align: "center"},
                {field: 'batch_size', width: 80, title: '批训练条数', sort: true, align: "center"},
                {field: 'last_train_time', width: 170, title: '最后训练时间', sort: true, align: "center"},
                {field: 'train_num', width: 100, title: '训练次数', align: "center"},
                {
                    field: 'status', width: 100, title: '训练状态', align: "center", templet(data) {
                        if (data.status && data.status === 1) {
                            return '<button type="button" class="layui-btn layui-btn-xs layui-btn-normal">未训练</button>'
                        }
                        if (data.status && data.status === 2) {
                            return '<button type="button" class="layui-btn layui-btn-warm layui-btn-xs">训练中</button>'
                        }
                        if (data.status && data.status === 3) {
                            return '<button type="button" class="layui-btn layui-btn-xs">训练完成</button>'
                        }
                        return data.status
                    }
                },
                {field: 'create_time', width: 170, title: '创建时间', sort: true, align: "center"},
                {
                    width: 150, title: '关联', align: "center", templet(data) {
                        let relevancy = data.relevancy
                        let tag = ''
                        for (let i = 0; i < relevancy.length; i++) {
                            tag += `<span class="layui-badge layui-bg-blue" >`+ relevancy[i] + ` </span></br>`
                        }
                        return tag
                    }
                },
                {
                    field: 'init_load', width: 100, title: '初始化加载', align: "center", templet(data) {
                        if (data.init_load && data.init_load === 1) {
                            return '<input type="checkbox" checked name="zzz" disabled lay-skin="switch" lay-filter="loadSwitch" lay-text="开启|关闭">'
                        }
                        return '<input type="checkbox" name="zzz" lay-skin="switch" disabled lay-filter="loadSwitch" lay-text="开启|关闭">'
                    }
                },
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            // limits: [10, 15, 20, 25, 50, 100],
            // limit: 15,
            // page: true,
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            layer.alert(result, {
                title: '最终的搜索信息'
            });

            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    searchParams: result
                }
            }, 'data');

            return false;
        });

        /**
         * toolbar事件监听
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {   // 监听添加操作
                var content = miniPage.getHrefContent('page/modules/continued_module_add.html');
                var openWH = miniPage.getOpenWidthHeight();

                // console.log(openWH)

                var index = layer.open({
                    title: '添加模型',
                    type: 1,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['50%', '80%'],
                    // area: [openWH[0] + 'px', openWH[1] + 'px'],
                    // offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                    end: function () {
                        table.reload('currentTableId')
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                layer.alert(JSON.stringify(data));
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            // console.log(data.corpus_determine)
            // if (data.corpus_determine === '1') {
            //     console.log('layui-btn-disabled')
            //     $('.gen_corpus').addClass('layui-btn-disabled')
            // }
            if (obj.event === 'edit') {

                var content = miniPage.getHrefContent('page/modules/continued_module_edit.html' + '?id=' + data.id);
                var openWH = miniPage.getOpenWidthHeight();

                var index = layer.open({
                    title: '编辑模型',
                    type: 1,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['50%', '80%'],
                    // area: [openWH[0] + 'px', openWH[1] + 'px'],
                    // offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                    end: function () {
                        table.reload('currentTableId')
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (index) {
                    obj.del();
                    layer.close(index);
                });
            } else if (obj.event === 'relevancy') {
                var content = miniPage.getHrefContent('page/modules/continued_module_relevancy.html');
                var openWH = miniPage.getOpenWidthHeight();

                // console.log(openWH)

                var index = layer.open({
                    title: '关联图书馆',
                    type: 1,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['70%', '70%'],
                    // area: [openWH[0] + 'px', openWH[1] + 'px'],
                    // offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'gen_corpus') {
                $.get('/admin/initcorpus/' + data.module_code + '/2', (res) => {
                    layer.msg('正在拉取语料，请耐心等待', {icon: 6});
                })
            }
        });

    });
</script>